package com.actionpals.components.containers
{
	import flash.display.DisplayObject;
	
	public class Grid extends Box
	{
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		/**
		 * Constructor.
		 */	
		public function Grid( columns:int = 10, rows:int = 10 , direction:String = BoxDirection.VERTICAL, spacing:int = 5 )
		{
			super( direction, spacing );
			_columns = columns;
			_rows = rows;
			_cells = rows * columns;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  columns
		//----------------------------------
		
		/**
		 * @private
		 * Storage for the columns property.
		 */
		private var _columns:int;
		
		/**
		 * The number of columns in the grid.
		 */
		public function get columns():int
		{
			return _columns;
		}
		
		/**
		 * @private
		 */
		public function set columns( value:int ):void
		{
			_columns = value;
		}
		
		//----------------------------------
		//  rows
		//----------------------------------
		
		/**
		 * @private
		 * Storage for the rows property.
		 */
		private var _rows:int;
		
		/**
		 * The number of rows in the grid.
		 */
		public function get rows():int
		{
			return _rows;
		}
		
		/**
		 * @private
		 */
		public function set rows( value:int ):void
		{
			_rows = value;
		}
		
		//----------------------------------
		//  cells
		//----------------------------------
		
		/**
		 * @private
		 * The total number of units allowed in the grid.
		 */
		private var _cells:int;
		
		/**
		 * The number of rows in the grid.
		 */
		public function get cells():int
		{
			return _cells;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden methods: Container
		//
		//--------------------------------------------------------------------------
		
		public override function arrange( index:int=0 ):void
		{
			var xPos:Number = 0;
			var yPos:Number = 0;
			var maxWidths:Array = new Array();
			var maxHeights:Array = new Array();
			var columnArray:Array = new Array();
			var rowArray:Array = new Array();
			var columnCount:Number = ( direction == BoxDirection.HORIZONTAL ) ? columns : Math.ceil( numChildren / rows );
			var rowCount:Number = ( direction == BoxDirection.HORIZONTAL ) ? Math.ceil( numChildren / columns ) : rows;
			var rowIncrementer:Number = 0;
			var columnIncrementer:Number = 0;
			var childIncrementer:Number = 0;
			var child:DisplayObject;
			
			for( var i:int = 0; i<( ( direction == BoxDirection.HORIZONTAL ) ? rowCount : columnCount ); i++ )
			{
				for( var j:int = 0; j<( ( direction == BoxDirection.HORIZONTAL ) ? columnCount : rowCount ); j++ )
				{
					try
					{
						child = getChildAt( childIncrementer );
					}
					catch( e:Error )
					{
						break;
					}
					
					if( !maxWidths[columnIncrementer] )
					{
						maxWidths[columnIncrementer] = 0;
					}
					if( !maxHeights[rowIncrementer] )
					{
						maxHeights[rowIncrementer] = 0;
					}
					if( !columnArray[columnIncrementer] )
					{
						columnArray[columnIncrementer] = new Array();
					}
					if( !rowArray[rowIncrementer] )
					{
						rowArray[rowIncrementer] = new Array();
					}
					if( child.width > maxWidths[columnIncrementer] )
					{
						maxWidths[columnIncrementer] = child.width;
					}
					if( child.height > maxHeights[rowIncrementer] )
					{
						maxHeights[rowIncrementer] = child.height;
					}
					rowArray[rowIncrementer].push( child );
					columnArray[columnIncrementer].push( child );
					if( direction == BoxDirection.HORIZONTAL )
					{
						columnIncrementer++;
					}
					else
					{
						rowIncrementer++;
					}
					childIncrementer++;
				}
				if( direction == BoxDirection.HORIZONTAL )
				{
					rowIncrementer++;
					columnIncrementer = 0;
				}
				else
				{
					columnIncrementer++;
					rowIncrementer = 0;
				}
			}
			
			for( i = 0; i < columnArray.length; i++ )
			{
				for( j = 0; j < columnArray[i].length; j++ )
				{
					columnArray[i][j].x = xPos;
				}
				xPos += maxWidths[i] + margin;
			}
			for( i = 0; i < rowArray.length; i++ )
			{
				for( j = 0; j < rowArray[i].length; j++ )
				{
					rowArray[i][j].y = yPos;
				}
				yPos += maxHeights[i] + margin;
			}
		}
		
	}
}